import Taro from '@tarojs/taro';
import u from 'updeep'
import request from '../util/request'

export default {
  namespace: 'rentRelease',
  state: {
    rentTypeList: ['厂房', '仓库', '土地'], // 求租类型下拉
    rentTypeArray: [{
      id: 'workshop',
      name: '厂房'
    }, {
      id: 'warehouse',
      name: '仓库'
    }, {
      id: 'land',
      name: '土地'
    }], // 求租类型下拉
    rentType:{
        id:'All',
        name: '不限'
      },
    rentTypeIndex: 'All', // 选中求租类型
    areaList: [], // 区域下拉
    areaData: [], // 选中区域
    districtTypeRange: [], // 条件
    districtType: {
      id: 0,
      name: '不限'
    },
    useTime: '', // 使用时间
    areaShow: false, // 是否展示区域多选模态框
    cellphone: '', // 联系电话
    minArea: '',
    maxArea: '',
    minPrice: '',
    maxPrice: '',
    contact: "",
    title: ''
  },
  reducers: {
    updateState(state, {payload}) {
      return u({
        ...payload
      }, state)
    }
  },
  effects: {
    // 获得区域信息
    * loadDistrict({payload = {}}, {put, call}) {
      const {data} = yield call(request, {
        url: SERVERURL + '/service/district/all',
        method: 'get',
      })

      if (data && data.data instanceof Array) {
        const d = data.data || {};
        yield put({
          type: 'updateState',
          payload: {
            districtTypeRange: d
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
    //添加
    * saveAddress({payload = {}}, {select, call}) {
      const {
        rentTypeArray, rentTypeIndex, districtType, cellphone, contact, title, description, endDate,
        minPrice, maxPrice, minArea, maxArea
      } = yield select(({rentRelease}) => rentRelease)
      let districtId = districtType.id;
      const param = {
        rentType: rentTypeArray[rentTypeIndex].id,
        districtId: districtId,
        cellphone: cellphone,
        contact: contact,
        title: title,
        minPrice: minPrice,
        maxPrice: maxPrice,
        minArea: minArea,
        maxArea: maxArea,
        description: description,
        endDate: endDate,

      }
      let url = "service/rent/add"

      const {data} = yield call(request, {
        url: SERVERURL + url,
        data: param,
        method: 'post',
      })
      if (data && data === "unauthc") {
        Taro.navigateTo({
          url: `/pages/login/index`
        })
        Taro.showToast({
          title: ("会话过时"),
          icon: 'none'
        })
      }

      if (data && data.code==='000000') {
        Taro.redirectTo({
          url: `/pages/businessHome/index`
        })
        Taro.showToast({
          title: (data || {}).msg || '添加成功',
          icon: 'none'
        })

      } else {
        Taro.showToast({
          title: (data || {}).msg || '添加失败',
          icon: 'none'
        })
      }
    }
  }
};
